var index;
$(function(){
  initTab("");
  initUserTab();
  downloadDataBtn();

  $.ajax({
    type: "get",
    url: "getNoticeListCount.do",
    success: function (data) {
      layui.use(['laypage', 'layer'], function() {
        var laypage = layui.laypage
            , layer = layui.layer;
        //总页数大于页码总数
        laypage.render({
          elem: 'demo1'
          , count: data //数据总数
          , limit:5
          , jump: function (obj) {
            var page = obj.curr;
            $.ajax({
              type: "get",
              url: "getNoticeList.do",
              data:{"page" : page,
                    "limit" : obj.limit},
              success: function (data1) {
                var html = "";
                $.each(data1,function (index,item) {
                  html+='<p>'+ (index + 1) +'&nbsp;-&nbsp;'+item.content+'-----'+item.createTime+'</p>';
                });
                if(html == ""){
                  html = "<p>无</p>";
                }
                $("#contentId").html(html);
              }
            });
          }
        });
      });
    }
  });

  });


function downloadDataBtn() {
  $("#profitTableDiv").on("click",".downloadData", function(){
    var opTime = $(this).attr("opTime");
    $.ajax({
      type: "get",
      url: "exportExcel.do",
      data: {
        "opTime": opTime
      },
      success: function (data) {
        window.location.href = 'downloadOrderExcel.do?fileName=' + data
      }
    });
  });
}




function initTab() {
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#profitTab'
      , height: '158'
      , url: 'getUserProfit.do'
      , cols: [[
        {field: 'account', align: 'center', width: '20%', title: '月份'}
        ,{field: 'profit', align: 'center', width: '30%', title: '业绩'}
        ,{field: 'updateTime', align: 'center', width: '30%', title: '更新时间'}
        ,{field: 'opTime', align: 'center', width: '20.1%', title: '操作',templet: function (data) {
          var loginUserRoleId = $("#loginUserRoleId").val();
          var html = '<a class="layui-btn layui-btn-xs" lay-event="edit">无</a>';
          if(loginUserRoleId == -1){
            html = '<a class="layui-btn layui-btn-xs downloadData" lay-event="edit" opTime='+data.account+' >下载明细</a>';
          }
          return html;
        }}
      ]]
    });
  })
}

function initUserTab() {
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#profitUserTab'
      , height: '290'
      , url: 'getUserProfitList.do'
      ,page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 5
        , limits: [5]
      }
      , cols: [[
        {field: 'userName', align: 'center', width: '15%', title: '用户名'}
        ,{field: 'roleName', align: 'center', width: '20%', title: '角色',
          templet: function (data) {
            return data.roleName + " (" + data.deptName + ")";
          }}
        ,{field: 'profitType', align: 'center', width: '15%', title: '业绩类型',
          templet: function (data) {
          if(data.profitType == 1){
            return "人才业绩";
          }else if(data.profitType == 2){
            return "企业业绩";
          }else {
            return "";
          }
        }}
        ,{field: 'account', align: 'center', width: '15%', title: '月份'}
        ,{field: 'profit', align: 'center', width: '15%', title: '业绩'}
        ,{field: 'updateTime', align: 'center', width: '20.2%', title: '更新时间'}
      ]]
    });
  })
}